/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package android.reasons;

import android.app.Activity;
import android.app.ContextoAndroid;
import android.app.FontManager;
import android.app.Mensaje;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.entidades.Usuario;
import android.facebook.FacebookApp;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

/**
 *
 * @author efraingutierrez
 */
public class Inicio extends Activity implements OnClickListener
{ 
    private LinearLayout abs;
    private ImageButton btnLocal,btnFace,btnSign;
    public static int RESULT_LOAD_IMAGE;
    private SignIn signin;
    public static final String APP_ID = "239360339588438";
    private FacebookApp face;
    private int caso = 3;
    private ProgressBar pb;
    
    public void handleAccess()
    {
        pb.setVisibility(ProgressBar.GONE);
        Usuario u = Usuario.isUser();
        if(u != null)
        {
            ContextoAndroid.usuario = u;
            ContextoAndroid.facebook = face;
            Intent i = new Intent(this,Wall.class);
            this.startActivity(i);
            this.overridePendingTransition(R.anim.fadein, R.anim.fadeout);
        }
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ContextoAndroid.context = this;
        startUI();
        FontManager.changeFont(abs);   
        handleAccess(); 
    }
    
    private void startUI()
    {
        this.setContentView(R.layout.inicio);
        abs = (LinearLayout)this.findViewById(R.id.absinicio);
        pb = (ProgressBar)this.findViewById(R.id.prog);
        pb.setVisibility(ProgressBar.GONE);
        abs.setBackgroundResource(R.drawable.turquesa);
        setImageButton(btnLocal,R.id.btnLocal);
        setImageButton(btnFace,R.id.btnFace);
        setImageButton(btnSign,R.id.btnSignIn);
    }
    
    private void setImageButton(ImageButton imgButton,int id)
    {
        imgButton = (ImageButton)this.findViewById(id);
        imgButton.setOnClickListener(this);
    }
   
    public void onClick(View arg0) 
    {
        if(arg0.getId() == R.id.btnLocal)
        {
            caso = 0;
            new LogIn(this);
        }
        else if(arg0.getId() == R.id.btnSignIn)
        {
            caso = 1;
            signin = new SignIn(this);
        }
        else
        {
            try{
                ApplicationInfo info = getPackageManager().
                getApplicationInfo("com.facebook.katana", 0 );
                caso = 2;
                face = new FacebookApp(this);
                pb.setVisibility(ProgressBar.VISIBLE);
                face.requestLogIn();
            } catch( PackageManager.NameNotFoundException e ){
                Mensaje.Mostrar("Error: La Aplicación de Facebook Debe Estar Instalada");
            }
                        
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        switch(caso)
        {
            case 1:
                signin.onActivityResult(requestCode,resultCode,data);
                break;
            case 2:
                face.getFace().authorizeCallback(requestCode, resultCode, data);
                face.initLocalUser();
                break;
        }
    }

    void back() 
    {
        startUI();
        handleAccess();
    }
    
}